package com.ruoyi.address.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.address.domain.AreaTree;
import com.ruoyi.address.domain.ChinaArea;
import com.ruoyi.address.domain.ParseResult;
import com.ruoyi.address.domain.vo.ChinaAreaWeChat;
import com.ruoyi.address.domain.vo.ChinaCascadeVo;
import com.ruoyi.address.domain.vo.ResolutionAddressResultVo;
import com.ruoyi.address.domain.vo.ResolutionAddressVo;
import com.ruoyi.address.mapper.ChinaAreaMapper;
import com.ruoyi.address.service.IChinaAreaService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.EasyCache;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 中国地区Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-27
 */
@Service
public class ChinaAreaServiceImpl implements IChinaAreaService {


    private static Logger logger = LoggerFactory.getLogger(ChinaAreaServiceImpl.class);

    @Autowired
    private ChinaAreaMapper chinaAreaMapper;


    @Autowired
    private IChinaAreaService chinaAreaService;

    /**
     * 查询中国地区
     *
     * @param areaCode 中国地区主键
     * @return 中国地区
     */
    @Override
    public ChinaArea selectChinaAreaByAreaCode(String areaCode) {
        return chinaAreaMapper.selectChinaAreaByAreaCode(areaCode);
    }

    @Override
    public ChinaArea selectChinaAreaByAreaCode2(String areaCode) {
        return chinaAreaMapper.selectChinaAreaByAreaCode(areaCode);
    }

    /**
     * 查询中国地区列表
     *
     * @param chinaArea 中国地区
     * @return 中国地区
     */
    @Override
    public List<AreaTree> selectChinaAreaList(ChinaArea chinaArea) {
        return chinaAreaMapper.selectChinaAreaList(chinaArea);
    }

    /**
     * 新增中国地区
     *
     * @param chinaArea 中国地区
     * @return 结果
     */
    @Override
    public int insertChinaArea(ChinaArea chinaArea) {
        return chinaAreaMapper.insertChinaArea(chinaArea);
    }

    /**
     * 修改中国地区
     *
     * @param chinaArea 中国地区
     * @return 结果
     */
    @Override
    public int updateChinaArea(ChinaArea chinaArea) {
        return chinaAreaMapper.updateChinaArea(chinaArea);
    }

    /**
     * 批量删除中国地区
     *
     * @param areaCodes 需要删除的中国地区主键
     * @return 结果
     */
    @Override
    public int deleteChinaAreaByAreaCodes(String[] areaCodes) {
        return chinaAreaMapper.deleteChinaAreaByAreaCodes(areaCodes);
    }

    /**
     * 删除中国地区信息
     *
     * @param areaCode 中国地区主键
     * @return 结果
     */
    @Override
    public int deleteChinaAreaByAreaCode(String areaCode) {
        return chinaAreaMapper.deleteChinaAreaByAreaCode(areaCode);
    }

    /**
     * 微信省市区
     */
    @Override
    public List<ChinaAreaWeChat> getChinaAreaWeChats() {
        List<AreaTree> provinceList = AddressParse.PROVINCE_LIST;
        List<ChinaAreaWeChat> areaWeChats = new ArrayList<>();
        for (AreaTree provice : provinceList) {
            ChinaAreaWeChat chinaAreaWeChat = new ChinaAreaWeChat();
            chinaAreaWeChat.setName(provice.getName());
            chinaAreaWeChat.setCode(provice.getAreaCode());
            List<ChinaAreaWeChat> cityLists = new ArrayList<>();
            for (AreaTree child : provice.getChildren()) {
                ChinaAreaWeChat city = new ChinaAreaWeChat();
                city.setName(child.getName());
                city.setCode(child.getAreaCode());
                List<ChinaAreaWeChat> areaLists = child.getChildren().stream().map(areaTree1 -> {
                    ChinaAreaWeChat area = new ChinaAreaWeChat();
                    area.setName(areaTree1.getName());
                    area.setCode(areaTree1.getAreaCode());
                    return area;
                }).collect(Collectors.toList());
                city.setAreaList(areaLists);
                cityLists.add(city);
            }
            chinaAreaWeChat.setCityList(cityLists);
            areaWeChats.add(chinaAreaWeChat);
        }
        return areaWeChats;
    }


    /**
     * 级联对象
     */
    @Override
    public List<ChinaCascadeVo> getChinaCascadeVos() {
        List<AreaTree> provinceList = AddressParse.PROVINCE_LIST;
        List<ChinaCascadeVo> chinaCascadeVos = new ArrayList<>();
        for (AreaTree provice : provinceList) {
            ChinaCascadeVo chinaCascadeVo = new ChinaCascadeVo();
            chinaCascadeVo.setLabel(provice.getName());
            chinaCascadeVo.setValue(provice.getAreaCode().toString());
            List<ChinaCascadeVo> childLists1 = new ArrayList<>();
            for (AreaTree child : provice.getChildren()) {
                ChinaCascadeVo city = new ChinaCascadeVo();
                city.setLabel(child.getName());
                city.setValue(child.getAreaCode().toString());
                List<ChinaCascadeVo> areaLists = child.getChildren().stream().map(areaTree1 -> {
                    ChinaCascadeVo area = new ChinaCascadeVo();
                    area.setLabel(areaTree1.getName());
                    area.setValue(areaTree1.getAreaCode().toString());
                    return area;
                }).collect(Collectors.toList());
                city.setChildren(areaLists);
                childLists1.add(city);
            }
            chinaCascadeVo.setChildren(childLists1);
            chinaCascadeVos.add(chinaCascadeVo);
        }
        return chinaCascadeVos;
    }

    /**
     * 解析地址
     * @param resolutionAddressVo
     */
    @Override
    public ResolutionAddressResultVo resolutionAddress(ResolutionAddressVo resolutionAddressVo) {
        String address = resolutionAddressVo.getAddress();
        if (StringUtils.isBlank(address)) {
            throw new ServiceException("解析地址不能为空！");
        }
        List<ParseResult> parseResults = AddressParse.parse(address);
        if (CollUtil.isEmpty(parseResults)) {
            throw new ServiceException("地址解析失败！");
        }
        for (ParseResult parseResult : parseResults) {
            logger.error(parseResult.format());
        }
        ResolutionAddressResultVo resultVo = new ResolutionAddressResultVo();
        ParseResult parseResult = parseResults.get(0);
        resultVo.setProjectLeader(parseResult.getName());
        resultVo.setProjectLeaderTelephone(parseResult.getMobile());
        resultVo.setProjectAddressProvinceCode(parseResult.getProvinceCode());
        resultVo.setProjectAddressProvinceName(parseResult.getProvince());
        resultVo.setProjectAddressCityCode(parseResult.getCityCode());
        resultVo.setProjectAddressCityName(parseResult.getCity());
        resultVo.setProjectAddressAreaCode(parseResult.getAreaCode());
        resultVo.setProjectAddressAreaName(parseResult.getArea());
        resultVo.setProjectSpecificAddress(parseResult.getDetail());
        return resultVo;
    }

    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public String selectByParam() {
        return chinaAreaMapper.selectByParam();
    }
}
